home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / REMOTETEST.PY < prev    next >
Encoding:
Python Source  |  1999-07-28  |  2.4 KB  |  86 lines

  1. """Demonstration of the Remote View protocol for adding
  2.    specially implemented Views in an application."""
  3. from gadfly import gadfly
  4.  
  5. # create the database
  6. g = gadfly()
  7. g.startup("dbtest", "dbtest")    # assume directory "dbtest" exists
  8.  
  9. # define a remote view class
  10. import gfintrospect
  11.  
  12. class myTable(gfintrospect.RemoteView):
  13.  
  14.     """A remote view must define self.column_names
  15.        to return a (fixed) list of string column names and
  16.        self.listing() to return a possibly varying
  17.        list of row values.  If there is a single column
  18.        the listing() list must return a list of values,
  19.        but for multiple columns it must return a list
  20.        of tuples with one entry for each column.
  21.        
  22.        The remote view implementation may optionally
  23.        redefine __init__ also, please see gfintrospect.py
  24.     """
  25.  
  26.     # static: don't reconstruct internal structure for each query
  27.     # for more interesting views static will generally be 0
  28.     static = 1
  29.    
  30.     def __init__(self, column_names=None, rowlist=None):
  31.         """do whatever needed for initialization"""
  32.         if column_names is None:
  33.             column_names = ['a', 'b', 'c']
  34.         if rowlist is None:
  35.             rowlist = [(1,2,3), (4,5,6), (7,8,9)]
  36.         self.column_names = column_names
  37.         self.rowlist = rowlist
  38.        
  39.     def listing(self):
  40.         """return list of tuples of right sizes to match column_names.
  41.            for more interesting views this will do something more
  42.            complex ;).
  43.         """
  44.         return self.rowlist
  45.     
  46. # create a table using default cols and rows
  47. ### Python code adding ANY remote views must be EXECUTED
  48. ### EACH TIME THE DATABASE LOADS!
  49.  
  50. g.add_remote_view("test", myTable())
  51.  
  52. # create a table using specified cols and rows
  53. # NOTE: for single column give list of values
  54. #   NOT list of tuples of values!
  55. g.add_remote_view("test2", myTable(["x"], [1,6,7]))
  56.  
  57. print g.database
  58.  
  59. c = g.cursor()
  60.  
  61. c.execute("select * from test")
  62. print "test::"
  63. print c.pp()
  64. print
  65. c.execute("select * from test2")
  66. print "test2::"
  67. print c.pp()
  68. print
  69. c.execute("select * from test, test2 where x=a")
  70. print "join"
  71. print c.pp()
  72. print
  73.  
  74. g.add_remote_view("test3", myTable(["z", "w"], [(2,3), (7,8), (4,6)]))
  75. c.execute("select * from test3")
  76. print "test3::"
  77. print c.pp()
  78. print
  79.  
  80. c.execute(
  81.  "select * from test, test2, test3 where x=a and z=b and w=c")
  82. print "join 2::"
  83. print c.pp()
  84. print
  85.  
  86.